import React from 'react';
import MainUI from '../../components/layout/MainUI';
import {connect} from 'dva';
import {
  Breadcrumb,
  Button,
  Col,
  Form,
  Modal,
  Pagination,
  Popconfirm,
  Row,
  Table,
  message,
  Dropdown,
  Menu,
  Icon
} from 'antd';
const params = {
  pageSize: 10,
  pageNumber: 1
};
class File extends React.Component {

  state = {
    columns: [
      {title: 'Key', dataIndex: 'key',},
      {title: '名称', dataIndex: 'oldFileName',},
      {title: '类型', dataIndex: 'fileType',},
      {title: '大小(byte)', dataIndex: 'fileSize',},
      {
        title: '操作', render: (text, row, index) => {
        const menu = <Menu>
          <Menu.Item key={"edit" + row.id}>
            <Button icon="edit" size="small" onClick={() => {
              this.editModalVisiblefuc(row);
            }}>查看</Button>
          </Menu.Item>
          <Menu.Divider/>
          <Menu.Item key={"delete" + row.id}>
            <Popconfirm title="你确定要进行删除操作?" onConfirm={() => {
              this.delete(row);
            }} okText="是" cancelText="否">
              <Button type='danger' icon="delete" size="small">删除</Button>
            </Popconfirm>
          </Menu.Item>
        </Menu>;
        return <Dropdown overlay={menu} trigger={['click']}>
          <a className="ant-dropdown-link" href="#">
            菜单 <Icon type="down"/>
          </a>
        </Dropdown>

      }
      },
    ],
  };
  search = (page, pageSize) => {
    if (page != null || page != undefined) {
      params.pageNumber = page;
    }

    this.props.form.validateFieldsAndScroll((errors, values) => {
      if (errors) {

      } else {
        this.props.dispatch({
          type: 'file/getPageFiles',
          payload: {...params, ...values}
        });
      }
    });
  };


  editModalVisiblefuc = (editRow) => {
    Modal.success({
      width:'800px',
      title: '查看',
      content: <div style={{maxWidth:'800px',maxHeight:'600px',overflow:'auto'}} ><img  src={editRow.url}  /></div>,
    });
  };

  delete = (row) => {
    if (row != undefined || row != null) {
      const keys = [row.key];
      this.props.dispatch({
        type: 'file/delete',
        payload: {keys: keys,params:params}
      });
      this.search();
    } else {
      message.warning('请选择需要删除的数据！');
    }
  };

  render() {

    return (
      <MainUI>
        <Breadcrumb style={{margin: '12px 0'}}>
          <Breadcrumb.Item>系统管理</Breadcrumb.Item>
          <Breadcrumb.Item> 文件管理</Breadcrumb.Item>
        </Breadcrumb>
        <div style={{padding: 24, background: '#fff', minHeight: 360}}>
          <Row>
            <Col span={24} style={{textAlign: 'right'}}>
              <Button type="primary" icon="retweet" onClick={() => {
                this.search();
              }}>刷新</Button>
            </Col>
          </Row>
          <Row>
            {<Table bordered={true} rowKey='id' style={{marginTop: '10px'}}
                    columns={this.state.columns}
                    dataSource={this.props.pageRows.data} pagination={false}/>}
            {
              this.props.pageRows.pageNumber != undefined ?
                <Pagination style={{float: 'right', marginTop: '5px'}} onChange={this.search}
                            total={this.props.pageRows.totalRows} current={this.props.pageRows.pageNumber}
                            pageSize={this.props.pageRows.pageSize}/> : ''
            }
          </Row>
        </div>
      </MainUI>
    )
  };
}

function mapStateToProps(state) {
  return {
    pageRows: state.file.pageRows,
  }
}

export default connect(mapStateToProps)(Form.create()(File));
